package Market;


import sim.engine.*;
import java.util.*;

public class ISO implements Steppable
{
	private static final long serialVersionUID = 6628525522939315274L;
	
													/**  Node Data : {number of buses, Penalty coefficient} */
	private double[][] NodeData;
													/** Branch Data : {From, To, Branch Capacity, Reactance} */
	private double[][] BranchData;	
													/** Hourly loadProfile Array = fixed demand : { each LSE demand over current hour } */
	private double[]   LoadProfile;
													/** Hourly loadProfile Double Array = fixed demand : { each LSE demand over current hour } */
	private double[][] LoadProfileD;
													/** True supply offer (a,b,CapTL,CapTU)  <br> @see DynTestAMES.JSLT.pdf */
	private double[][] TSupplyOffer; 
													/** Reported supply offer (aR,bR,CapRL,CapRU) <br> @see DynTestAMES.JSLT.pdf*/
	private double[][] RSupplyOffer; 
													/** GenCo's Data: {Gen ID, at Node, FCost, aT, bT, CapTL, CapTU, Initial Money in pounds} */
	private double[][] GenCoData;
													/** LSE's Data: {LSE ID, at Node, L(0), L(1),L(2),L(3),L(4), ...,L(23)} */
	private double[][] LSEData;
	
	//int hour;
	
	private ArrayList GenCoList;
	
	private Market              market;
	private Initials.InitNode   initnode;
	private Initials.InitBranch initbranch;
	private OptimalPowerFlow    opf;
	
	  public Hashtable<String, Hashtable> reportedSupplyOffer = new Hashtable<String, Hashtable>();
	  public Hashtable<String, Hashtable> electricityDemand = new Hashtable<String, Hashtable>();
	  public Hashtable<String, Hashtable<String, Double>> branchList = new Hashtable<String, Hashtable<String, Double>>() ;
	  public Hashtable<String, Double> nodeData = new Hashtable<String, Double>() ;
	  
//C-O-N-S-T-R-U-C-T-O-R------------------------------------------------------------------------------------------------------------------------------  
	//public ISO(Hashtable reportedsupplyoffer, Hashtable electricitydemand, Hashtable branchlist, Hashtable nodedata,int h, Market mt){
	  public ISO(Market mt)
	  {		
		market = mt;
	  }
	
//-----------------------------------------------------------------------------------------------------------------------------------------
    public void step(SimState state)
  {
    //System.out.println("Iso1 ");
   	
    for (int hour=0; hour<market.Hours; hour++)
    {
   	
    	NodeData     = new double[1][2];
    	BranchData   = new double[5][4];
    	LoadProfileD = new double[24][3];
    	LoadProfile  = new double[3];
    	TSupplyOffer = new double[market.genCoList.size()][4];
    	RSupplyOffer = new double[market.genCoList.size()][4];
    	GenCoData    = new double[market.genCoList.size()][8];
    	
    	for (int i=0; i<market.genCoList.size(); i++)
    	{    		
    		int id = i+1;
    		GenCo gen = market.genCoList.get("genco"+id);
    		GenCoData[i][0] = id; 
    		GenCoData[i][1] = gen.node;
    		GenCoData[i][2] = gen.fcost;
    		GenCoData[i][3] = gen.supplyOffer.get("aT");
    		GenCoData[i][4] = gen.supplyOffer.get("bT");
    		GenCoData[i][5] = gen.supplyOffer.get("capTL");
    		GenCoData[i][6] = gen.supplyOffer.get("capTU");
    		GenCoData[i][7] = gen.cumulativeWealth;
    	
    		TSupplyOffer[i][0] = gen.supplyOffer.get("aT");
    		TSupplyOffer[i][1] = gen.supplyOffer.get("bT");
    		TSupplyOffer[i][2] = gen.supplyOffer.get("capTL");
    		TSupplyOffer[i][3] = gen.supplyOffer.get("capTU");   	
    	}
    	
    	
    	for (int j=0; j<market.lseList.size(); j++)
    	{
    		int id = j+1;
    		LoadProfile[j] = market.lseList.get("lse"+id).electricityDemand.get(Integer.toString(hour));
    	}
    	
		NodeData[0][0] =  market.nodeData.get("buses");
		NodeData[0][1] =  market.nodeData.get("penalty");
		
     	
    	for (int n=0; n<market.branchList.size(); n++)
    	{
    		int id = n+1;
    		//Initials.InitBranch initbranch = market.branchList.get("branch"+id);
    		
    		Hashtable<String, Double> BranchHash = new Hashtable<String, Double>();
    		BranchHash =(Hashtable) market.branchList.get("branch"+id);
    		    		
    		BranchData[n][0] = ((Double)BranchHash.get("from")).doubleValue();
    		BranchData[n][1] = ((Double)BranchHash.get("to")).doubleValue();
    		BranchData[n][2] = ((Double)BranchHash.get("capacity")).doubleValue();
    		BranchData[n][3] = ((Double)BranchHash.get("reactance")).doubleValue();

    	}    	
    	opf = new OptimalPowerFlow(market,hour);
    }//end of out for loop
   }//end of step function
 }//class